package com.ruoyi.dispatch.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;

import com.ruoyi.dispatch.domain.MiningTeam;
import com.ruoyi.dispatch.domain.MonthYield;
import com.ruoyi.dispatch.domain.WorkfaceInfo;
import com.ruoyi.dispatch.enums.WorkfaceDayReportCol;
import com.ruoyi.dispatch.service.IMiningTeamService;
import com.ruoyi.dispatch.service.IWorkfaceInfoService;
import com.ruoyi.dispatch.vo.WorkfaceDayReportVO;
import com.ruoyi.framework.utils.BeanCopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dispatch.domain.WorkfaceDayReport;
import com.ruoyi.dispatch.service.IWorkfaceDayReportService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产量日报Controller
 *
 * @author wuxiaofang
 * @date 2025-04-08
 */
@Api("产量日报")
@RestController
@RequestMapping("/dispatch/workfaceDayReport")
@Anonymous
public class WorkfaceDayReportController extends BaseController
{
    @Autowired
    private IWorkfaceDayReportService workfaceDayReportService;
    @Autowired
    private IWorkfaceInfoService workfaceInfoService;
    @Autowired
    private IMiningTeamService miningTeamService;

    /**
     * 查询产量日报列表
     */
   // @PreAuthorize("@ss.hasPermi('dispatch:workfaceDayReport:list')")
    @ApiOperation("获取产量日报列表")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", dataTypeClass = Integer.class,required = true,example = "1"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", dataType = "Integer", dataTypeClass = Integer.class,required = true,example = "10"),
    })
    public TableDataInfo list(WorkfaceDayReport workfaceDayReport,String[] columnArray)
    {
        startPage();
        List<WorkfaceDayReport> list = workfaceDayReportService.selectWorkfaceDayReportList(workfaceDayReport);
        //组装展示列名信息
        List<Map<String,String>> columns = null;
        if(columnArray!=null && columnArray.length!=0){
            columns = workfaceDayReportService.handleColumnMap(columnArray);
        }
        TableDataInfo tableData =  getDataTable(list);
        tableData.setColumns(columns);

        return tableData;
    }

    /**
     * 导出产量日报列表
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:workfaceDayReport:export')")
    @Log(title = "产量日报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorkfaceDayReport workfaceDayReport)
    {
        List<WorkfaceDayReport> list = workfaceDayReportService.selectWorkfaceDayReportList(workfaceDayReport);
        ExcelUtil<WorkfaceDayReport> util = new ExcelUtil<WorkfaceDayReport>(WorkfaceDayReport.class);
        util.exportExcel(response, list, "产量日报数据");
    }

    /**
     * 获取产量日报详细信息
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:workfaceDayReport:query')")
    @GetMapping(value = "/{reportId}")
    public AjaxResult getInfo(@PathVariable("reportId") Long reportId)
    {
        return success(workfaceDayReportService.selectWorkfaceDayReportByReportId(reportId));
    }

    /**
     * 新增产量日报
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:workfaceDayReport:add')")
    //@Log(title = "产量日报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkfaceDayReport workfaceDayReport)
    {
       // workfaceDayReport.setCreateBy(getUsername());
        //临时兼容处理 根据工作面/队组名称查id
        if(workfaceDayReport.getWorkfaceName()!=null){
            WorkfaceInfo workfaceInfo = new WorkfaceInfo();
            workfaceInfo.setWorkFaceName(workfaceDayReport.getWorkfaceName());
            List<WorkfaceInfo> workfaceList = workfaceInfoService.selectWorkfaceInfoList(workfaceInfo);
            if(workfaceList.size()>0){
                workfaceDayReport.setWorkfaceId(workfaceList.get(0).getWorkfaceId());
            }
        }
        if(workfaceDayReport.getMiningTeamName()!=null){
            MiningTeam param = new MiningTeam();
            param.setName(workfaceDayReport.getMiningTeamName());
            List<MiningTeam> miningTeamList = miningTeamService.selectDispatchMiningTeamList(param);
            if(miningTeamList.size()>0){
                workfaceDayReport.setMiningTeamId(miningTeamList.get(0).getMiningTeamId());
            }
        }
        //插入数据
        int insertResult = workfaceDayReportService.insertWorkfaceDayReport(workfaceDayReport);


        //查询此项新增的记录
        WorkfaceDayReport record = workfaceDayReportService.selectWorkfaceDayReportByReportId(workfaceDayReport.getReportId());
        WorkfaceDayReportVO vo = BeanCopyUtils.copyProperties(record,WorkfaceDayReportVO.class);
        //组装默认展示的列信息
        vo.setDisplayCols(WorkfaceDayReportCol.getDefalutCols());


        return toAjax(insertResult,vo);
    }

    /**
     * 修改产量日报
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:workfaceDayReport:edit')")
    @Log(title = "产量日报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkfaceDayReport workfaceDayReport)
    {
       // workfaceDayReport.setUpdateBy(getUsername());
        return toAjax(workfaceDayReportService.updateWorkfaceDayReport(workfaceDayReport));
    }

    /**
     * 删除产量日报
     */
    //@PreAuthorize("@ss.hasPermi('dispatch:workfaceDayReport:remove')")
    @Log(title = "产量日报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{reportIds}")
    public AjaxResult remove(@PathVariable Long[] reportIds)
    {
        return toAjax(workfaceDayReportService.deleteWorkfaceDayReportByReportIds(reportIds));
    }
    @RequestMapping("countYield")
    public List<MonthYield> countYield(WorkfaceDayReport workfaceDayReport){
        return workfaceDayReportService.countMonthYield(workfaceDayReport);
    }
}
